# 使用 Maven 构建阶段
FROM maven:3.8.4-openjdk-17-slim AS build

# 设置工作目录
WORKDIR /build

# 创建 Maven 配置目录
RUN mkdir -p /root/.m2

# 设置 Maven 镜像源 - 使用多个镜像源以提高依赖获取成功率
RUN echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0">' > /root/.m2/settings.xml && \
    echo '  <mirrors>' >> /root/.m2/settings.xml && \
    echo '    <mirror>' >> /root/.m2/settings.xml && \
    echo '      <id>aliyun</id>' >> /root/.m2/settings.xml && \
    echo '      <mirrorOf>central</mirrorOf>' >> /root/.m2/settings.xml && \
    echo '      <url>https://maven.aliyun.com/repository/public</url>' >> /root/.m2/settings.xml && \
    echo '    </mirror>' >> /root/.m2/settings.xml && \
    echo '    <mirror>' >> /root/.m2/settings.xml && \
    echo '      <id>huaweicloud</id>' >> /root/.m2/settings.xml && \
    echo '      <mirrorOf>central</mirrorOf>' >> /root/.m2/settings.xml && \
    echo '      <url>https://mirrors.huaweicloud.com/repository/maven/</url>' >> /root/.m2/settings.xml && \
    echo '    </mirror>' >> /root/.m2/settings.xml && \
    echo '  </mirrors>' >> /root/.m2/settings.xml && \
    echo '  <profiles>' >> /root/.m2/settings.xml && \
    echo '    <profile>' >> /root/.m2/settings.xml && \
    echo '      <id>default</id>' >> /root/.m2/settings.xml && \
    echo '      <repositories>' >> /root/.m2/settings.xml && \
    echo '        <repository>' >> /root/.m2/settings.xml && \
    echo '          <id>central</id>' >> /root/.m2/settings.xml && \
    echo '          <url>https://repo.maven.apache.org/maven2</url>' >> /root/.m2/settings.xml && \
    echo '        </repository>' >> /root/.m2/settings.xml && \
    echo '      </repositories>' >> /root/.m2/settings.xml && \
    echo '    </profile>' >> /root/.m2/settings.xml && \
    echo '  </profiles>' >> /root/.m2/settings.xml && \
    echo '  <activeProfiles>' >> /root/.m2/settings.xml && \
    echo '    <activeProfile>default</activeProfile>' >> /root/.m2/settings.xml && \
    echo '  </activeProfiles>' >> /root/.m2/settings.xml && \
    echo '</settings>' >> /root/.m2/settings.xml

# 增加 Maven 可用内存并设置临时目录
ENV MAVEN_OPTS="-Xmx1024m -Djava.io.tmpdir=/tmp"

# 确保有足够的磁盘空间
RUN apt-get update && apt-get clean && \
    rm -rf /var/lib/apt/lists/* && \
    find /root/.m2 -name "*.lock" -type f -delete

# 复制所有相关的 pom.xml
COPY pom.xml ./pom.xml
COPY tutor-common/pom.xml ./tutor-common/pom.xml
COPY tutor-util/pom.xml ./tutor-util/pom.xml

# 首先安装父 POM
RUN mvn -f pom.xml install -N -DskipTests

# 复制依赖模块的源代码
COPY tutor-common/src ./tutor-common/src

# 构建并安装 common 模块
RUN mvn -f tutor-common/pom.xml clean install -DskipTests

# 复制当前模块的源代码
COPY tutor-util/src ./tutor-util/src

# 执行当前模块构建
RUN mvn -f tutor-util/pom.xml clean install -DskipTests

# 由于 util 是一个依赖模块，不需要单独运行，所以这里不需要运行阶段
# 这个 Dockerfile 仅用于构建和安装到 Maven 本地仓库 